package com.doit.spark.day05

import com.doit.spark.day01.utils.SparkUtil

import scala.collection.mutable

/**
 * @DATE 2022/1/8/11:22
 * @Author MDK
 * @Version 2021.2.2
 * */
object WordCount {
  def main(args: Array[String]): Unit = {
    val sc = SparkUtil.getSc
    val words = sc.textFile("data/word.txt").flatMap(_.split("\\s+"))
    val acc = new MyAccumalator
    sc.register(acc, "myAcc")

    words.foreach(word=>{
      //输入单词 累加器统计次数
      acc.add(word)
    })
      //获取累加器的结果
      val res: mutable.HashMap[String, Int] = acc.value
      res.toList.foreach(println)
  }
}
